package com.todo.event.activity.application.handler.fieldtransfer;

import com.alibaba.fastjson.JSONObject;
import com.todo.event.activity.application.core.engine.qlexp.QLExpressManager;
import com.todo.event.activity.application.entity.context.FieldTransferContext;
import com.todo.event.activity.common.enums.EventFieldDataSourceEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author azhebuxing
 * @date 2025/1/9 19:38
 * @description
 */
@Slf4j
@Component
public class QLExpressHandler extends AbstractFieldTransferHandle{

    @Resource
    private QLExpressManager qlExpressManager;

    @Override
    protected String doTransfer(FieldTransferContext context) {
        try {
            Object execute = qlExpressManager.execute(context.getTransfer(), context.getPayload());
            return String.valueOf(execute);
        }catch (Exception ex){
            log.error("CodeTransferHandler#doTransfer error context:【{}】", JSONObject.toJSONString(context), ex);
        }
        return null;
    }

    @Override
    protected Boolean doMatch(FieldTransferContext context) {
        return context.getTransfer() != null;
    }

    @Override
    protected EventFieldDataSourceEnum getHandleType() {
        return EventFieldDataSourceEnum.QLEXPRESS;
    }
}
